package com.kingkim.electromechanical.usercenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kingkim.electromechanical.commonutils.model.PageBean;
import com.kingkim.electromechanical.commonutils.util.Common;
import com.kingkim.electromechanical.usercenter.mapper.MenuMapper;
import com.kingkim.electromechanical.usercenter.model.po.Menu;
import com.kingkim.electromechanical.usercenter.service.IMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MenuServiceImpl implements IMenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public PageBean QueryForList(int page, int limit, Menu menu) throws DataAccessException {
        PageHelper.startPage(page,limit);
        List menuList = menuMapper.QueryForList(menu);
        PageInfo pi = new PageInfo<Menu>(menuList);
        PageBean pageBean = new PageBean<Menu>();
        pageBean.setCurrentPage(pi.getPageNum());
        pageBean.setPageCount(pi.getPages());
        pageBean.setList(pi.getList());
        pageBean.setTotalCount(pi.getTotal());
        return pageBean;
    }

    @Override
    public List QueryParentForList() throws DataAccessException {
        return menuMapper.QueryParentForList();
    }

    @Override
    public Integer DeleteForList(List<String> ids) throws DataAccessException {
        return menuMapper.DeleteForList(ids);
    }

    @Override
    public int SaveOrModify(Menu menu) throws DataAccessException {
        if(StringUtils.isNotBlank(menu.getSystem_menu_id())){
            return menuMapper.Modify(menu);
        }else{
            menu.setSystem_menu_id(Common.GetUUID());
            return menuMapper.Save(menu);
        }
    }
}
